import React, { useState } from 'react'
import { Drawer, Modal, message } from "antd"
import { useSelector, useDispatch } from "umi";
import $http from "@/services";
import "./index.less"
import IconMap from "../IconMap"
export default function DrawerDetail({ title, render, reloadFunc, _id, interfaced, isStaff = false }) {
    const { isShowDetail } = useSelector(state => state.common);
    const [DelModalStatu, setDelModalStatu] = useState(false);//控制Modal是否显示，主要原因为其当没传入时会有bug
    const dispatch = useDispatch();
    // 关闭
    const onClose = () => {
        dispatch({
            type: "common/setIsShowDetail",
            payload: false
        })
    }

    // 删除其内容
    const _deleteItem = async () => {
        const params = isStaff ? { ids: [_id] } : { id: _id };
        const { code, msg } = await $http[interfaced](params);
        if (code || msg === "删除失败,ID值错误") {
            onClose();
            setDelModalStatu(false);
            return message.error(msg);
        }
        message.success(msg);
        setDelModalStatu(false);
        onClose();
        reloadFunc(1);

    }

    // 修改提示框
    const isDelModal = () => {
        setDelModalStatu(!DelModalStatu);
    }

    const extra = (
        <>
            <span className="icon" onClick={isDelModal}>{IconMap.del}</span>
            <span className="line"></span>
            <span className="icon" onClick={onClose}>{IconMap.close}</span>
        </>
    )
    const titleNode = (
        <>
            <span className='icon'>{IconMap.copy}</span>
            <span className='title'>{title}</span>
        </>
    )


    return (
        <Drawer
            title={titleNode}
            placement="right"
            onClose={onClose}
            open={isShowDetail}
            width={800}
            closable={false}
            extra={extra}
        >
            <Modal
                title="温馨提示?"
                onOk={_deleteItem}
                open={DelModalStatu}
                closeIcon={IconMap.wran}
                onCancel={() => { isDelModal(!DelModalStatu) }}
            >确定要删除当前信息么?</Modal>
            {render()}
        </Drawer>
    )
}
